﻿var dm_closeCallback; // 外部关闭回调
var dm_closeInnerCallback; // 内部关闭回调
var dm_dataCache; // 传入变量缓存

// 注册数据
function dm_registerData(data, key) {
	if (key == null) key = 'top';
	if (dm_registerData == null)
		dm_registerData = new Array();
	dm_registerData[key] = data;
}

// 获取注册的数据
function dm_getData(key) {
	if (!dm_registerData)
		return null;

	return dm_registerData[key] || null;
}

function dm_registerCallback(closeCbFun, key) {
	if (key == null) key = 'top';
	if (dm_closeCallback == null)
		dm_closeCallback = new Array();
	dm_closeCallback[key] = closeCbFun;
}

function dm_registerInnerCallback(closeCBFun, key) {
	if (key == null) key = 'top';
	if (dm_closeInnerCallback == null)
		dm_closeInnerCallback = new Array();
	dm_closeInnerCallback[key] = closeCBFun;
}

function dm_fire(needRefresh, key, data) {
	if (!dm_closeCallback) return;
	if (key == null) key = 'top';

	var func = null;

	// 先调用内部回调
	if (dm_closeInnerCallback) {
		func = dm_closeInnerCallback[key];
		if (func)
			func(needRefresh, data);
		delete dm_closeInnerCallback[key];
	}

	// 再调外部回调
	if (dm_closeCallback) {
		func = dm_closeCallback[key];
		if (func)
			func(needRefresh, data);
		delete dm_closeCallback[key];
	}

	// 销毁注册的数据
	if (dm_registerData) {
		delete dm_registerData[key];
	}
}

function dm_showDialog(selector, key, title, data, options) {
    var op = $.extend({}, {
        onBeforeInitialize: null,
		onInitialize: null,
		okCallback: null,
		cancelCallback: null,
		width: 300,
		height: 300,
		padding: 2
    }, options);

    dm_registerData(data, key);

    if (op.onBeforeInitialize)
        op.onBeforeInitialize(key);

	var content = $(selector).html();
	$(selector).html('');
	var lastContent = content;
	var realContent = content.replace('${KEY}', '\'' + key + '\'');
	while (realContent != lastContent) {
		lastContent = realContent;
		realContent = realContent.replace('${KEY}', '\'' + key + '\'');
	}

	var closed = false;

	var d = dialog({
		title: title,
		content: realContent,
		width: op.width,
		height: op.height,
		padding: op.padding,
		onshow: function () {
			dm_registerCallback(function (needRefresh, data) {
				if (d) d.remove();

				$(selector).html(content);

				var callback = (needRefresh ? op.okCallback : op.cancelCallback);
				if (callback)
					callback(data);
            }, key);

            if (op.onInitialize)
                op.onInitialize(key);
		},
		onclose: function () {
			dm_fire(false, key);
		}
	});
	d.showModal();
}

/********** 通用多选对话框 **********/

// items 应当具有形式:
//      [ { ID: ..., Name: ..., Selected: true/false }, { ... }, ... ]
// callback: function(needRefresh, results)
// results 具有和items一样的形式
function dm_ShowMultipleSelectDialog(title, items, callback) {
    var selector = '#dMultipleSelect';
    dm_showDialog(selector, 'dMultipleSelect', null, items, {
        onBeforeInitialize: function (key) {
            dm_dMSRefreshItems(key, selector);
        },
        okCallback: function (results) {
            if (callback)
                callback(true, results);
        },
        cancelCallback: function () {
            if (callback)
                callback(false);
        }
    });
}

/***** 对话框内部 *****/

function dm_dMSRefreshItems(key, selector) {
    var data = dm_getData(key);
    var container = $('.dMSContainer');
    var html = [];
    for (var i = 0; data && i < data.length; i++) {
        var item = data[i];
        var selected = (!!item.Selected ? ' selected="selected"' : '');
        html.push('<div class="dMSItem" tagID="' + item.ID + '" tagName="' + data[i].Name + '"' + selected + ' onclick="dm_dMSItemClick(\'' + selector + '\', this, \'' + key + '\')">' + item.Name + '</div>');
    }
    container.html(html.join('\r\n'));
}

function dm_dMSItemClick(selector, element, key) {
    var id = $(element).attr('tagID');
    var data = dm_getData(key);

    var item = null;
    for (var i = 0; data && i < data.length; i++) {
        var e = data[i];
        if (e.ID != id)
            continue;

        item = e;
        break;
    }
    if (!e)
        return;

    item.Selected = !item.Selected;
    dm_dMSRefreshItems(key, selector);
}

function dm_dMSOK(key) {
    var elements = $('.dMSContainer .dMSItem');
    var results = [];
    for (var i = 0; elements && i < elements.length; i++) {
        var element = $(elements[i]);
        results.push({
            ID: element.attr('tagID'),
            Name: element.attr('tagName'),
            Selected: !!element.attr('selected')
        });
    }
    dm_fire(true, key, results);
}

function dm_dMSCancel(key) {
    dm_fire(false, key);
}